﻿//============================================================
// Project: DigitalFactory
// Author: Administrator@DESKTOP-IE3FPT7
// Datetime: 2021-03-23 18:13:17
// Description: TODO >> This is a script Description.
//============================================================

using Sirenix.OdinInspector;
using System.Collections.Generic;
using UnityEngine;

namespace NodePathFinder.Script
{
    /// <summary>
    /// 为了性能。只检查一级节点。
    /// </summary>
    [RequireComponent(typeof(NodeTypeMapProxy))]
    public class NodeTypeMapEntity:MonoBehaviour
    {
        /// <summary>
        /// 节点
        /// </summary>
        public List<Vector3> Nodes { get; private set; }
        /// <summary>
        /// 边，x：起节点序号，y:终节点序号
        /// </summary>
        [GUIColor(.5f,.5f,0)]
        [InfoBox("此物体下的子物体位置会被视为节点。子节点名称为节点序号。以此序号配置边。")]
        public List<Vector2Int> Edges;
        private NodeTypeMapProxy _nodeTypeMapProxy;

        private void Awake()
        {
            Nodes = new List<Vector3>();
            InitNodeByEmptyChid();
            _nodeTypeMapProxy = GetComponent<NodeTypeMapProxy>();
            _nodeTypeMapProxy.Init(Nodes.Count);
            foreach (var item in Edges)
            {
                float distance = Vector3.Distance(Nodes[item.x], Nodes[item.y]);
                _nodeTypeMapProxy.AddEdge(new Edge((int)item.x, (int)item.y, distance));
            }
        }

        public int[] PathFind(int start,int end) 
        {
            return _nodeTypeMapProxy.PathFind(start, end);
        }

        private void InitNodeByEmptyChid() 
        {
            for (int i = 0; i < transform.childCount; i++)
            {
                Nodes.Add( transform.GetChild(i).position);
            }
        }
    }
}
